﻿using Client.MVVM;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using ZCustomControl.Models;

namespace WPFCustomControlExample.ViewModel
{
    public class ZTreeViewExampleViewModel : ViewModelBase
    {
        #region Fields

        private SearchModel _selectedSearchItem = null;
        private ViewItemModel _treeSelectedItem = null;

        #endregion

        #region Properties

        public ObservableCollection<ViewItemModel> DataCollection { get; set; }
        public ObservableCollection<SearchModel> SearchSource { get; set; }

        public SearchModel SelectedSearchItem
        {
            get { return _selectedSearchItem; }
            set
            {
                _selectedSearchItem = value;
                if(_selectedSearchItem != null && DataCollection != null)
                {
                    ViewItemModel vim = DataCollection.FirstOrDefault(p => p.Id == _selectedSearchItem.Id);
                    TreeSelectedItem = vim;
                }
                RaisePropertyChanged("SelectedSearchItem");
            }
        }

        public ViewItemModel TreeSelectedItem
        {
            get { return _treeSelectedItem; }
            set
            {
                _treeSelectedItem = value;
                RaisePropertyChanged("TreeSelectedItem");
            }
        }

        #endregion

        #region Constructors

        public ZTreeViewExampleViewModel()
        {
            DataCollection = new ObservableCollection<ViewItemModel>();
            SearchSource = new ObservableCollection<SearchModel>();

            for(int i = 0; i < 100000; i++)
            {
                ViewItemModel hostVim = new ViewItemModel();
                hostVim.Id = "TestData" + i;
                hostVim.ViewItemName = "TestData" + i;
                hostVim.Icon = @"/Images/treeicon.png";
                hostVim.IconBackground = new SolidColorBrush(Colors.Cyan);

                SearchModel sm1 = new SearchModel();
                sm1.Id = "TestData" + i;
                sm1.Name = "TestData" + i; 
                sm1.SearchField = "TestData" + i;
                sm1.Tag = hostVim;
                SearchSource.Add(sm1);

                ViewItemModel childVim = new ViewItemModel();
                childVim.Id = "Child" + i;
                childVim.ViewItemName = "Child" + i;
                childVim.Icon = @"/Images/treeicon.png";
                childVim.IconBackground = new SolidColorBrush(Colors.DarkCyan);

                SearchModel sm2 = new SearchModel();
                sm2.Id = "Child" + i;
                sm2.Name = "Child" + i;
                sm2.SearchField = "Child" + i;
                sm2.Tag = "Child" + i;
                SearchSource.Add(sm2);

                ViewItemModel grandChildVim = new ViewItemModel();
                grandChildVim.Id = "GrandChild" + i;
                grandChildVim.ViewItemName = "GrandChild" + i;
                grandChildVim.Icon = @"/Images/treeicon.png";
                grandChildVim.IconBackground = new SolidColorBrush(Colors.Green);

                childVim.Children.Add(grandChildVim);
                hostVim.Children.Add(childVim);
                DataCollection.Add(hostVim);
            }
        }

        #endregion
    }
}
